<?php


namespace App\Http\Controllers\Admin;
use App\Models\Banner;
use App\Models\Credit;
use Illuminate\Http\Request;

class CreditController extends BaseController
{

    public function index(Request $request) {
        $field = ['nickname', 'avatar_url', 'phone', 'id_card', 'title', 'education_num', 'end_date'];
        $title = $request->input('title');
        $phone = $request->input('phone');
        $type = $request->input('type');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $users = Credit::withCertain('user', $field)->when($title, function ($q) use($title){
            $q->whereHas('user', function ($qt)  use($title){
                $qt->where('title', 'like', '%' . $title . '%');
            });
        })->when($phone, function ($q) use($phone){
            $q->whereHas('user', function ($qt)  use($phone){
                $qt->where('phone', 'like', '%' . $phone . '%');
            });
        })->when($type != -1, function ($q) use($type){
            $q->where('type', $type);
        })->when($start_at, function ($q) use($start_at){
            $q->where('date', '>=', $start_at);
        })->when($end_at, function ($q) use($end_at){
            $q->where('date', '<=', $end_at);
        })->orderBy("id", "desc")
            ->paginate(\request('per_page', 10));
        return $this->paginate($users);
    }

}

